import random

from EE.models import GameState
from EE.models import Contract
from EE.models import AcceptedContracts
from EE.models import ReleasedContracts
from EE.models import VolatileContracts

from EE.logic import decision as decision_logic

def releaseFirst(ref_user):
  contract = createNew(50000, 50, 100)
  acceptedContract = setAccepted(contract, ref_user)
  
def createNew(ref_pop, ref_use, ref_price):
  contract = Contract(noOfCitizens=ref_pop, usage=ref_use, price=ref_price)
  contract.save()
  return contract
  
def setAccepted(ref_contract, ref_user):
  contract = AcceptedContracts(noOfCitizens=ref_contract.noOfCitizens, usage=ref_contract.usage, price=ref_contract.price)
  contract.user = ref_user
  contract.finalOffer = ref_contract.price
  contract.save()
  Contract.objects.filter(id=ref_contract.id).delete()
  return contract

def releaseNewRound(ref_game):
  length = Contract.objects.all().count()
  number = random.randrange(1, length, 1)
  contract = Contract.objects.all()[number]
  released = release(contract, ref_game)
  return released
  
def release(ref_contract, ref_game):
  contract = ReleasedContracts(noOfCitizens=ref_contract.noOfCitizens, usage=ref_contract.usage, price=ref_contract.price)
  contract.game = ref_game
  contract.save()
  #Contract.objects.filter(id=ref_contract.id).delete()
  return contract
  
def get_total_usage(ref_user):
  accepted = AcceptedContracts.objects.filter(user=ref_user).all()
  total_usage = 0
  for c in accepted:
    total_usage = total_usage + c.usage
  return total_usage

def get_average_price(ref_user):
  if AcceptedContracts.objects.filter(user=ref_user).count() == 0:
    return 0
  accepted = AcceptedContracts.objects.filter(user=ref_user).all()
  total_price = 0
  for c in accepted:
    total_price = total_price + (c.price)
  average_price = 0
  number_of_contracts = AcceptedContracts.objects.filter(user=ref_user).count()
  average_price = total_price / number_of_contracts
  return average_price
  
def make_offer(ref_id, contract_type, offer, ref_state):
  decision = decision_logic.getDecision(ref_state)
  accepted = False
  if (contract_type == 'released'):
    old_contract = ReleasedContracts.objects.filter(id=ref_id).get()
    ReleasedContracts.objects.filter(id=ref_id).delete()
    new_contract = VolatileContracts(decision=decision)
    new_contract.noOfCitizens = old_contract.noOfCitizens
    new_contract.usage = old_contract.usage
    new_contract.price = old_contract.price
    new_contract.bid = offer
    new_contract.save()
    accepted = True
  else:
    new_contract = VolatileContracts.objects.filter(id=ref_id).get()
    if (new_contract.bid < offer):
	  new_contract.decision = decision
	  new_contract.bid = offer
	  accepted = True
	
  return new_contract, accepted
  
def handle_end_round(ref_decision):
  contract = VolatileContracts.objects.filter(decision=ref_decision).all()
  for c in contract:
    accepted = AcceptedContracts(user=c.decision.game.user)
    accepted.finalOffer = c.bid
    accepted.noOfCitizens = c.noOfCitizens
    accepted.usage = c.usage
    accepted.price = c.price
    accepted.save()
    VolatileContracts.objects.filter(id=c.id).delete()
  